package org.dftproject.genesis.ui.presentation.tabcontrol;

import org.dftproject.genesis.ui.Activator;
import org.dftproject.genesis.ui.figures.ISelectionListener;
import org.dftproject.genesis.ui.figures.TooltipLabel;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jface.action.LegacyActionTools;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;

public class TabControlFigure extends Figure {
	
	private final static Color borderColor = new Color(null, 0x55, 0x55, 0x55);
	
	public TabControlFigure(final TabControl tabControl) {
		ToolbarLayout layout = new ToolbarLayout(true);
		layout.setSpacing(1);
		setLayoutManager(layout);
		
		TabButtonFigure newTabFigure = new TabButtonFigure(this);
		newTabFigure.setClosable(false);
		newTabFigure.setIcon(Activator.getImageDescriptor("icons/page_white.png").createImage());
		newTabFigure.setToolTip(new TooltipLabel("New Tab (" + LegacyActionTools.findModifierString(SWT.CTRL) +"+T)"));
		newTabFigure.addSelectionListener(new ISelectionListener() {

			public void selected() {
				tabControl.newTab();
			}
			
		});
		add(newTabFigure);
	}
	
	public TabFigure addTabItem(final TabItem item) {
		TabFigure figure = new TabFigure(this, item);
		figure.addSelectionListener(new ISelectionListener() {

			public void selected() {
				item.setSelection();
			}
			
		});
		add(figure, getChildren().size() - 1);
		return figure;
	}
	
	@Override
	public void paintFigure(Graphics graphics) {
		super.paintFigure(graphics);
		
		Rectangle bounds = getBounds();
		graphics.setForegroundColor(borderColor);
		graphics.drawLine(bounds.x, bounds.bottom() - 1, bounds.right(), bounds.bottom() - 1);
	}

	public Color getBorderColor() {
		return borderColor;
	}
	
}
